
from cgitb import text
from operator import truediv
from random import randint
from sqlite3 import Row
from textwrap import fill
from tkinter import *
import csv
import tkinter

from click import command


class Maze:

    # 读取maze_map.csv文件，得到地图，然后显示
    def __init__(self) -> None:
        self.maze_map = []

        # 读入地图
        with open('maze_map.csv', 'r') as f:
            f_csv = csv.reader(f)
            for row in f_csv:
                self.maze_map.append(row)

        self.size = len(self.maze_map)
        self.pos = [0, 0]

        self.maze_map[0][self.size-1] = '2'

        self.window = Tk()
        self.window.title('maze game')
        self.window.geometry('1000x1500')

        self.window.bind('<Up>', self.up_event)
        self.window.bind('<Down>', self.down_event)
        self.window.bind('<Left>', self.left_event)
        self.window.bind('<Right>', self.right_event)

        self.cv = Canvas(self.window, bg='white', width=1000, height=1500)

        self.labels = []

    # 渲染整个棋盘
    def render(self) -> None:
        for i in range(len(self.maze_map)):
            for j in range(len(self.maze_map[0])):
                if self.maze_map[i][j] == '0':     # road
                    bg = 'white'
                elif self.maze_map[i][j] == '1':   # wall
                    bg = 'gray'
                elif self.maze_map[i][j] == '2':   # start
                    bg = 'blue'
                elif self.maze_map[i][j] == '3':   # end
                    bg = 'red'
                else:                              # diamond
                    bg = 'pink'
                id = self.cv.create_rectangle(
                    j*100, i*100, j*100 + 100, i*100+100, fill=bg)

        # up = self.cv.create_rectangle(
        #     400, 1100, 500, 1200, fill='brown', tags='up')
        # down = self.cv.create_rectangle(
        #     400, 1300, 500, 1400, fill='green', tags='down')
        # left = self.cv.create_rectangle(
        #     300, 1200, 400, 1300, fill='pink', tags='left')
        # right = self.cv.create_rectangle(
        #     500, 1200, 600, 1300, fill='yellow', tags='right')

        # self.cv.tag_bind(up, '<Button-1>', self.up_event)
        # self.cv.tag_bind(down, '<Button-1>', self.down_event)
        # self.cv.tag_bind(left, '<Button-1>', self.left_event)
        # self.cv.tag_bind(right, '<Button-1>', self.right_event)

        self.cv.pack()

    def mainloop(self) -> None:
        self.window.mainloop()

    def up_event(self, event):
        print('up')
        x, y = self.pos
        if self.legalCheck(self.pos, 'up') == False:
            return
        item = self.cv.find_closest(x, y)
        self.cv.itemconfigure(item, fill="white")

        next_item = self.cv.find_closest(x, y-100)
        self.cv.itemconfigure(next_item, fill="blue")

        # 更新self.maze_map
        self.maze_map[int(x/100)][int(y/100)] = '0'
        self.maze_map[int(x/100)][int(y/100-1)] = '2'

        # 更新self.pos
        self.pos = x, y-100
        x, y = x, y-100
        if (x/100, y/100) == (self.size-1, self.size-1):
            print("win")
        self.pos = x, y

        print(self.pos)

    def down_event(self, event):
        print('down')
        x, y = self.pos
        if self.legalCheck(self.pos, 'down') == False:
            return
        item = self.cv.find_closest(x, y)
        self.cv.itemconfigure(item, fill="white")

        next_item = self.cv.find_closest(x, y+100)
        self.cv.itemconfigure(next_item, fill="blue")

        # 更新self.maze_map
        self.maze_map[int(x/100)][int(y/100)] = '0'
        self.maze_map[int(x/100)][int(y/100+1)] = '2'

        x, y = x, y+100
        if (x/100, y/100) == (self.size-1, self.size-1):
            print("win")
        self.pos = x, y
        print(self.pos)

    def left_event(self, event):
        print('left')
        x, y = self.pos
        if self.legalCheck(self.pos, 'left') == False:
            return
        item = self.cv.find_closest(x, y)
        self.cv.itemconfigure(item, fill="white")

        next_item = self.cv.find_closest(x-100, y)
        self.cv.itemconfigure(next_item, fill="blue")

        # 更新self.maze_map
        self.maze_map[int(x/100)][int(y/100)] = '0'
        self.maze_map[int(x/100-1)][int(y/100)] = '2'

        x, y = x-100, y
        if (x/100, y/100) == (self.size-1, self.size-1):
            print("win")
        self.pos = x, y
        print(self.pos)

    def right_event(self, event):
        print('right')
        x, y = self.pos
        if self.legalCheck(self.pos, 'right') == False:
            return
        item = self.cv.find_closest(x, y)
        self.cv.itemconfigure(item, fill="white")

        next_item = self.cv.find_closest(x+100, y)
        self.cv.itemconfigure(next_item, fill="blue")

        # 更新self.maze_map
        self.maze_map[int(x/100)][int(y/100)] = '0'
        self.maze_map[int(x/100+1)][int(y/100)] = '2'

        x, y = x+100, y
        if (x/100, y/100) == (self.size-1, self.size-1):
            print("win")
        self.pos = x, y
        print(self.pos)

    def legalCheck(self, pos, direction) -> bool:
        # 边界
        x, y = pos
        if direction == 'up' and y == 0:
            return False
        if direction == 'down' and y / 100 == self.size-1:
            return False
        if direction == 'left' and x == 0:
            return False
        if direction == 'right' and x / 100 == self.size-1:
            return False

        # 判断是否能走
        if direction == 'up' and self.maze_map[int(x/100)][int(y/100-1)] not in ['0', '3']:
            return False
        if direction == 'down' and self.maze_map[int(x/100)][int(y/100+1)] not in ['0', '3']:
            return False
        if direction == 'left' and self.maze_map[int(x/100-1)][int(y/100)] not in ['0', '3']:
            return False
        if direction == 'right' and self.maze_map[int(x/100+1)][int(y/100)] not in ['0', '3']:
            return False
        return True


if __name__ == "__main__":
    maze = Maze()
    maze.render()
    maze.mainloop()
